//
//  FourteenViewController.m
//  项目二
//
//  Created by rimi on 16/5/17.
//  Copyright © 2016年 objective. All rights reserved.
//

#import "FourteenViewController.h"
#import "UIColor+Expand.h"
#define SCREEN_W CGRectGetWidth(self.view.bounds)
@interface FourteenViewController ()<UIScrollViewDelegate>
@property(nonatomic,strong)UIScrollView *scrollView;
@property(nonatomic)BOOL animation;
@property(nonatomic,strong)UIPageControl *pageControl;
@property(nonatomic,strong)NSTimer *timer;
@end

@implementation FourteenViewController
- (void)dealloc{
    [_timer invalidate];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"滚动视图、分页控件";
    [self initUIInterface];
    /**关闭导航栏透视效果*/
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"上" style:UIBarButtonItemStylePlain target:self action:@selector(leftItem)];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"下" style:UIBarButtonItemStylePlain target:self action:@selector(rightItem)];
    
    /**播放，暂停*/
    
    NSArray *buttonTitleArray = @[@"播放", @"暂停"];
    for (NSInteger i = 0; i < 2; i ++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.frame = CGRectMake(20 + 230 * i, 600, 80, 30);
        [button setTitle:buttonTitleArray[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(buttonTaped:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 200 + i;
        [self.view addSubview:button];
    }
    
    /**初始化timer*/
    _timer = [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(xxxxx) userInfo:nil repeats:YES];
    /**暂停timer*/
    _timer.fireDate = [NSDate distantFuture];
    
    
}
#pragma mark -UIButton
-(void)buttonTaped:(UIButton *)sender{
   
    NSInteger index = sender.tag - 200;
    if ( index == 0) {
        //开始播放
        //distantPast (开始timer)
        _timer.fireDate = [NSDate distantPast];
        
    } else {
        //结束播放
        _timer.fireDate = [NSDate distantFuture];
    }
}
#pragma mark -timer
-(void)xxxxx{
   [self leftItem];
}

/**界面*/
-(void)initUIInterface{
    [self.view addSubview:self.scrollView];
    [self.view addSubview:self.pageControl];
    for (NSInteger index = 0; index < 8; index ++) {
        UILabel *label = [[UILabel alloc]initWithFrame:CRM(SCREEN_W *index, 0, SCREEN_W, SCREEN_W)];
        label.font = [UIFont systemFontOfSize:30];
        label.textAlignment = NSTextAlignmentCenter;
        label.text = [NSString stringWithFormat: @"滚动第%ld页",index];
        label.backgroundColor = [UIColor specialRandomColor];
        [_scrollView addSubview:label];
    }
   
}
#pragma mark - UIBarButtonItem
-(void)leftItem{
    if (_animation) {
        return;
    }
    _animation = YES;
    
    if (_scrollView.contentOffset.x > 0) {
        [_scrollView setContentOffset:CPM(_scrollView.contentOffset.x -SCREEN_W, 0) animated:YES];
    }else if (_scrollView.contentOffset.x == 0){
    /**最后一页直接跳第一页*/
        [_scrollView setContentOffset:CGPointMake(SCREEN_W * 7, 0) animated:YES];
    }
    [self performSelector:@selector(radsad) withObject:nil afterDelay:0.5];
    
}

-(void)rightItem{
    if (_animation) {
        return;
    }
    _animation = YES;
    
    if (_scrollView.contentOffset.x < SCREEN_W * 7) {
        [_scrollView setContentOffset:CPM(_scrollView.contentOffset.x -SCREEN_W, 0) animated:YES];
    }else if (_scrollView.contentOffset.x == SCREEN_W * 7){
        /**最后一页直接跳第一页*/
        [_scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    [self performSelector:@selector(radsad) withObject:nil afterDelay:0.5];
}

-(void)radsad{
    _animation = NO;
}
#pragma mark - get

-(UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc]init];
        _scrollView.bounds  = CRM(0, 0, SCREEN_W, SCREEN_W);
        _scrollView.center = self.view.center;
        _scrollView.backgroundColor = [UIColor lightGrayColor];
        /**设置滚动视图内容的大小*/
        _scrollView.contentSize = CSM(SCREEN_W *8, SCREEN_W);
        /**设置分页*/
        _scrollView.pagingEnabled = YES;
        /**是否显示指示条*/
        _scrollView.showsHorizontalScrollIndicator = NO;
        /**设置代理*/
        _scrollView.delegate = self;
    }
    return _scrollView;
}

- (UIPageControl *)pageControl{
    if (!_pageControl) {
        _pageControl = [[UIPageControl alloc]init];
        _pageControl.bounds = CRM(0, 0, SCREEN_W, 30);
        _pageControl.center = CPM(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)+150);
        /**总页数*/
        _pageControl.numberOfPages = 8;
        /**当前指示的数*/
        _pageControl.currentPage = 0;
        /**设置当前指示器小圆点的颜色*/
        _pageControl.currentPageIndicatorTintColor = [UIColor redColor];
    }
    return _pageControl;
}


#pragma mark -- scrollview delegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSLog(@"%@",NSStringFromCGPoint(scrollView.contentOffset));
    /*
     round(): 如果参数是小数，则对自身四舍五入
     ceil():  如果参数是小数，则取最大整数
     floor(): 如果参数是小数，则取小整数
     */
    NSInteger currentPage = round(scrollView.contentOffset.x / SCREEN_W);
    _pageControl.currentPage = currentPage;
    
    //    if ((NSInteger)scrollView.contentOffset.x % (NSInteger)SCREEN_W == 0) {
    //        _isAnimating = NO;
    //    }
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"will begin dragging");
}
@end
